package greendbgui

class PersonTagLib 
{
    static namespace = 'pe'

    def person =
    {
        attrs ->
        
        def personId = attrs['id']
        
//        out << "<p>Id: " + personId + "</p>"
                             
        Person p = Person.get(personId)
        
        out << "<p>(" + p.id + ") " + p.surname + ", " + p.firstNames
        
        if (p.birthday18 != null)
        {
            out << "(18th: " + p.birthday18 + ")"
        }
        
        out << "</p>"
    }
    
    def addresses =
    {
        attrs ->
        
        def entryId = attrs['id']
        
//        out << "<p>Id: " + personId + ". rollId: " + rollId + "</p>"
        
        ElectoralRollEntry e = ElectoralRollEntry.get (entryId)
        
        Person p = e.person
        
        String title = "Current Address"
        
        def rollEntries = p.electoralRollEntry.sort {it -> it.electoralRoll.date}.reverse()
        
        Address currentAddress = rollEntries.get(0).address
        List<ElectoralRoll> currentRolls = new LinkedList<ElectoralRoll>()
        currentRolls.add (rollEntries.get(0).electoralRoll)
        
        rollEntries.remove(0)
        
        
        for (rollEntry in rollEntries)
        {
            if (rollEntry.address.equals (currentAddress))
            {
                currentRolls.add (rollEntry.electoralRoll)
            }
            else
            {
                outputCurrentAddress (title, currentAddress, currentRolls)
                
                title = "Previous Address"
                currentAddress = rollEntry.address
                currentRolls.add (rollEntry.electoralRoll)
            }
        }
        
        outputCurrentAddress (title, currentAddress, currentRolls)
    }
    
    def outputCurrentAddress (title, address, rolls)
    {
        out << "<h3>" << title << "</h3>"
        
        out << "<p>"
        for (line in address.getMultiLine())
        {
            out << line << "<br/>"
        }
        out << "</p>"
        
        out << "<p>"
        for (roll in rolls)
        {
            out << roll << "<br/>"
        }
        out << "</p>"
    }
}
